package planit.view;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import planit.controller.ControllerFactory;
import planit.controller.ControllerFactory.ControllerType;
import planit.model.Event;
import planit.model.EventType;
import planit.model.Model;
import planit.model.Person;
import planit.view.wrappers.EventWrapper;
import planit.view.wrappers.ModelCellRenderer;
import planit.view.wrappers.PersonWrapper;

import com.toedter.calendar.JDateChooser;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class EditEventDialog extends javax.swing.JDialog {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel selectLabel;
	private JList eventList;
	private JScrollPane jScrollPane1;
	private JLabel descriptionLabel;
	private AbstractAction cancelAction;
	private JButton cancelButton;
	private JTextField descriptionTextField;
	private JTextField nameTextField;
	private JLabel nameLabel;
	private JButton saveButton;
	private JPanel jPanel1;
	private AbstractAction cancelAbstract;
	private JLabel durationLabel;
	private JLabel startLabel;
	private JSpinner durationSpinner;
	private JSpinner startTimeSpinner;
	private JLabel jLabel1;
	private JDateChooser jDateChooser1;
	private JTextField typeTextField;
	private JButton jButton1;
	private JButton jButton2;
	private JTextField placeTextField;
	private JLabel typeLabel;
	private JLabel PlaceLabel;
	
	
	private Model model;
	private JLabel placeLabel;
	private JTextField typeField;
	private JTextField placeField;
	private JLabel dateLabel;
	private JLabel startTimeLabel;
	private JSpinner durationPanel;
	private JSpinner startTimePanel;

	public EditEventDialog(Model model) {
		super();
		this.model = model;
		initGUI();
		updateEventList();
		
	}
	
	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setResizable(false);
				{
					selectLabel = new JLabel();
					getContentPane().add(selectLabel);
					selectLabel.setText("Select an Event");
					selectLabel.setBounds(6, 6, 93, 16);
				}
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(6, 22, 81, 188);
					{
						ListModel eventListModel = 
							new DefaultComboBoxModel(
									new String[] { "Item One", "Item Two" });
						eventList = new JList();
						jScrollPane1.setViewportView(eventList);
						eventList.setModel(eventListModel);
						eventList.setBounds(6, 22, 81, 188);
						eventList.setPreferredSize(new java.awt.Dimension(75, 180));
						eventList.addListSelectionListener(new ListSelectionListener() {
							public void valueChanged(ListSelectionEvent evt) {
								eventlistValueChanged(evt);
							}
						});
					}
				}
				{
					saveButton = new JButton();
					getContentPane().add(saveButton);
					saveButton.setAction(ControllerFactory
							.getController(ControllerType.EDIT_EVENT));
					saveButton.getAction().putValue("EditEventDialog", this);
					saveButton.setText("Save");
					saveButton.setBounds(0, 269, 81, 40);
				}
				{
					nameLabel = new JLabel();
					getContentPane().add(nameLabel);
					nameLabel.setText("Name:");
					nameLabel.setBounds(93, 22, 37, 16);
				}
				{
					nameTextField = new JTextField();
					getContentPane().add(nameTextField);
					nameTextField.setBounds(93, 38, 187, 28);
				}
				{
					descriptionLabel = new JLabel();
					getContentPane().add(descriptionLabel);
					descriptionLabel.setBounds(93, 72, 68, 14);
					descriptionLabel.setText("Description");
				}
				{
					descriptionTextField = new JTextField();
					getContentPane().add(descriptionTextField);
					descriptionTextField.setBounds(93, 86, 187, 28);
				}
				{
					cancelButton = new JButton();
					getContentPane().add(cancelButton);
					getContentPane().add(getJDateChooser1());
					getContentPane().add(getStartTimePanel());
					getContentPane().add(getDurationPanel());
					getContentPane().add(getStartTimeLabel());
					getContentPane().add(getDurationLabel());
					getContentPane().add(getDateLabel());
					getContentPane().add(getPlaceField());
					getContentPane().add(getTypeField());
					getContentPane().add(getTypeLabel());
					getContentPane().add(getPlaceLabel());
					cancelButton.setAction(getCancelAction());
					cancelButton.setText("Cancel");
					cancelButton.setBounds(210, 281, 70, 28);
				}
			}
			pack();
			this.setSize(311, 353);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void eventlistValueChanged(ListSelectionEvent evt) {
		if(eventList.getSelectedValue() == null) {
			nameTextField.setText("");
			descriptionTextField.setText("");
			
		} else {
			Event temp;
			temp = model.getSchedule().getEvent(((EventWrapper)eventList
					.getSelectedValue())
					.getId());
			
			nameTextField.setText(temp.getName());
			descriptionTextField.setText(temp.getDescription());
			this.placeField.setText(temp.getPlace());
			this.typeField.setText("");
			this.jDateChooser1.setDate(temp.getStartTime());
			Calendar tempCal = Calendar.getInstance();
			tempCal.set(0,0,0, 0, temp.getDuration());
			SpinnerDateModel model = new SpinnerDateModel();
			durationPanel.setModel(model);
			JComponent editor = new JSpinner.DateEditor(durationPanel, "HH:mm");
			durationPanel.setEditor(editor);
			durationPanel.setValue(tempCal.getTime());
			model = new SpinnerDateModel();
			this.startTimePanel.setModel(model);
			editor = new JSpinner.DateEditor(startTimePanel, "HH:mm");
			tempCal.setTime(temp.getStartTime());
			
			this.startTimePanel.setEditor(editor);
			this.startTimePanel.setValue(tempCal.getTime());
			updateEventPersonsList();
			
		}
	}
	
	public void updateEventList(){
		Event[] event = new Event[0];
		ArrayList<JLabel> allEvents = new ArrayList<JLabel>();
		event = model.getSchedule().getEvents().toArray(event);
		for (int i = 1; i < event.length; i++) {
			allEvents.add(new EventWrapper(event[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allEvents.toArray());
		eventList.setModel(list2Model);
		eventList.setCellRenderer(new ModelCellRenderer());
	}

	public void updateEventPersonsList(){
		Event temp;
		temp = model.getSchedule().getEvent(((EventWrapper)eventList
				.getSelectedValue())
				.getId());
		
		Person[] person = new Person[0];
		ArrayList<JLabel> allPersons = new ArrayList<JLabel>();
		person = temp.getPersons().toArray(person);
		for (int i = 0; i < person.length; i++) {
			allPersons.add(new PersonWrapper(person[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allPersons.toArray());
		
	}
	
	private AbstractAction getCancelAction() {
		if(cancelAction == null) {
			cancelAction = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return cancelAction;
	}
	
	public int getEventId() {
		return ((EventWrapper)eventList.getSelectedValue()).getId();
	}
	
	
	public String getEventName() {
		return nameTextField.getText();
	}
	
	public String getEventDescription() {
		return descriptionTextField.getText();
	}
	public EventType getEventType() {
		return new EventType(); 
	}

	public String getEventPlace() {
		return placeField.getText();
	}


	public Date getEventDuration() {
		return (Date)durationPanel.getValue();
	}

	public Date getEventDate() {
		return jDateChooser1.getDate();
	}

	public Date getEventStartTime() {
		return (Date)startTimePanel.getValue();
	}
	
	private JDateChooser getJDateChooser1() {
		if(jDateChooser1 == null) {
			jDateChooser1 = new JDateChooser();
			jDateChooser1.setBounds(138, 247, 125, 28);
		}
		return jDateChooser1;
	}
	
	private JSpinner getStartTimePanel() {
		if(startTimePanel == null) {
			SpinnerListModel startTimePanelModel = 
				new SpinnerListModel(
						new String[] { "Sun", "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" });
			startTimePanel = new JSpinner();
			startTimePanel.setModel(startTimePanelModel);
			startTimePanel.setBounds(191, 190, 56, 29);
		}
		return startTimePanel;
	}
	
	private JSpinner getDurationPanel() {
		if(durationPanel == null) {
			SpinnerListModel durationPanelModel = 
				new SpinnerListModel(
						new String[] { "Sun", "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" });
			durationPanel = new JSpinner();
			durationPanel.setModel(durationPanelModel);
			durationPanel.setBounds(191, 219, 56, 28);
		}
		return durationPanel;
	}
	
	private JLabel getStartTimeLabel() {
		if(startTimeLabel == null) {
			startTimeLabel = new JLabel();
			startTimeLabel.setText("Starttime:");
			startTimeLabel.setBounds(99, 197, 52, 16);
		}
		return startTimeLabel;
	}
	
	private JLabel getDurationLabel() {
		if(durationLabel == null) {
			durationLabel = new JLabel();
			durationLabel.setText("Duration:");
			durationLabel.setBounds(99, 225, 52, 16);
		}
		return durationLabel;
	}
	
	private JLabel getDateLabel() {
		if(dateLabel == null) {
			dateLabel = new JLabel();
			dateLabel.setText("Date:");
			dateLabel.setBounds(103, 256, 29, 16);
		}
		return dateLabel;
	}
	
	private JTextField getPlaceField() {
		if(placeField == null) {
			placeField = new JTextField();
			placeField.setBounds(161, 156, 119, 28);
		}
		return placeField;
	}
	
	private JTextField getTypeField() {
		if(typeField == null) {
			typeField = new JTextField();
			typeField.setBounds(161, 122, 119, 28);
		}
		return typeField;
	}
	
	private JLabel getTypeLabel() {
		if(typeLabel == null) {
			typeLabel = new JLabel();
			typeLabel.setText("Type:");
			typeLabel.setBounds(93, 128, 29, 16);
		}
		return typeLabel;
	}
	
	private JLabel getPlaceLabel() {
		if(placeLabel == null) {
			placeLabel = new JLabel();
			placeLabel.setText("Place:");
			placeLabel.setBounds(93, 162, 34, 16);
		}
		return placeLabel;
	}

}
